<template>
  <div class="test-page">
    <Header />
    <div class="page-header">
      <h2>心理问卷</h2>
      <p class="subtitle">通过专业的心理测评问卷，了解自己的心理健康状况</p>
    </div>
    
    <!-- 测试列表 -->
    <div class="test-list" v-loading="loading">
      <el-card v-for="questionaire in questionaires" 
        :key="questionaire.questionaireId" 
        class="test-card"
      >
        <div class="test-header">
          <h3>{{ questionaire.title }}</h3>
        </div>
        
        <div class="test-description">
          {{ questionaire.introduction }}
        </div>
        
        <div class="test-footer">
          <el-button 
            type="primary" 
            @click="startQuestionaire(questionaire.questionaireId)"
            class="start-button"
          >
            开始问卷
            <el-icon class="el-icon--right"><ArrowRight /></el-icon>
          </el-button>
        </div>
      </el-card>
    </div>
    <Footer />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getQuestionaireList, type Questionaire } from '../../api/test'
import Header from '../../components/header.vue'
import Footer from '../../components/footer.vue'
import { ArrowRight } from '@element-plus/icons-vue'

const router = useRouter()
const loading = ref(false)
const questionaires = ref<Questionaire[]>([])

// 获取问卷列表
const fetchQuestionaires = async () => {
  loading.value = true
  try {
    const res = await getQuestionaireList()
    if (res.code === '200') {
      questionaires.value = res.data
    }
  } catch (error) {
    console.error('获取问卷列表失败:', error)
    ElMessage.error('获取问卷列表失败')
  } finally {
    loading.value = false
  }
}

// 开始问卷
const startQuestionaire = (questionaireId: number) => {
  router.push(`/questionaire/${questionaireId}`)
}

onMounted(() => {
  fetchQuestionaires()
})
</script>

<style scoped>
.test-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
}

.page-header {
  text-align: center;
  margin: 64px auto 20px;
  max-width: 1200px;
  padding: 0 20px;
}

.page-header h2 {
  color: #2c3e50;
  margin-bottom: 12px;
  font-size: 32px;
}

.subtitle {
  color: #606266;
  font-size: 16px;
  margin: 0;
}

.test-list {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px 40px;
  flex: 1;
}

.test-card {
  width: 800px; /* 假设这是原来宽度的两倍 */
  margin-bottom: 30px;
  transition: all 0.3s;
  border-radius: 10px;
  padding: 50px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.test-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
}

.test-header {
  margin-bottom: 20px;
  border-bottom: 1px solid #ebeef5;
  padding-bottom: 20px;
}

.test-header h3 {
  margin: 0;
  font-size: 24px;
  color: #2c3e50;
  line-height: 1.4;
}

.test-description {
  margin: 20px 0 30px;
  color: #333;
  font-size: 16px;
  line-height: 1.6;
  min-height: 72px;
  padding-left: 16px;
  border-left: 4px solid #409EFF;
}

.test-footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-top: 20px;
  border-top: 1px solid #ebeef5;
  padding-top: 20px;
}

.start-button {
  width: 140px;
  height: 40px;
  font-size: 16px;
  border-radius: 20px;
  transition: all 0.3s;
}

.start-button:hover {
  transform: translateX(5px);
  background-color: #66b1ff;
}

.test-category {
  display: none;
}
</style>
